package com.mars.ray.utils;

import org.rosuda.JRI.REXP;
import org.rosuda.JRI.RList;
import org.rosuda.JRI.RVector;
import org.rosuda.JRI.Rengine;

public class CallRLib {
	public static void main( String[] args ){
        //System.out.println( "call R themis!" );
		String geneFile = "D:\\Rworks\\demo\\gene.txt";
		String expFile = "D:\\Rworks\\demo\\data\\ACC_norm.txt";
		String directory = "D:\\Rworks\\demo\\result";
		String path = "D:\\Rworks\\demo\\result\\sum data";
        themis(expFile,geneFile,directory,path);
		//themis();
		//callFunction();
    }
	
	private static void callFunction(){
		String filePath = "D:\\Rworks\\cell.R";
		// 初始化R解析类
		Rengine engine = new Rengine(null, false, null);
        System.out.println("rJava引擎初始化完成，开始加载R");
        if (!engine.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }
        engine.assign("fileName", filePath);
        // 在R中执行文件。执行后，文件中的两个函数加载到R环境中
        engine.eval("source(fileName)");
        System.out.println("R文件执行完毕");
        // 直接调用无参的函数，将结果保存到一个对象中
        REXP rexp = engine.eval("atom ()");
        System.out.println(rexp);
        double d = rexp.asDouble();
        System.out.println("---------------R 无参数的方法atom ()，返回值="+d);
        System.out.println("---------------R 无参数的方法atom ()，调用结束");
        
        // 定义一个数组，与R中c1集合对应
        String[] arr1 = new String[]{"a", "b", "c"};
        // 将数组复制给R中的变量c1。R中变量无需预先定义
        engine.assign("c1", arr1);

        // 定义一个数组，与R中c2集合对应
        double[] arr2 = new double[]{1, 2, 3};
        // 将数组复制给R中的变量c2
        engine.assign("c2", arr2);
        // 将c1 c2连接为一个集合（R中的数据集，类似java的list），赋值给一个变量
        engine.eval("x <- data.frame(c1, c2)");
        // 将一个数值保存到一个变量中
        int num = 10;
        engine.eval("y <- "+num); 
        // 参数list，返回值为list。调用R中函数，将结果保存到一个对象中。
        System.out.println("---------------R 有参数的方法molecule ()，传入的参数1="+arr1[0]+","+arr1[1]+","+arr1[2]);
        System.out.println("---------------R 有参数的方法molecule ()，传入的参数2="+arr2[0]+","+arr2[1]+","+arr2[2]);
        System.out.println("---------------R 有参数的方法molecule ()，传入的参数3="+num);
        REXP rexpParam = engine.eval("molecule(x, y)");
        System.out.println("---------------R 有参数的方法molecule ()，返回值="+rexpParam);
        System.out.println("---------------R 有参数的方法molecule ()，调用结束");
        engine.end();
        
        
	}
	
	public static void themis(){
		String filePath = "D:\\Rworks\\Themis2\\Run Demo.R";
		Rengine engine = new Rengine(null, false, null);
		if (!engine.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }
		engine.assign("fileName", filePath);
		System.out.println("task started .....");
		engine.eval("source(fileName)");
		System.out.println("task end .....");
		//REXP rexp = engine.eval("ConsensusClusterPlusAnalysis(expFile, geneFile, directory,maxK = 9, reps = 1000, pItem = 0.8,clusterNum = 2,clusterAlg = \"km\", distance = \"pearson\", pFeature = 1,seed = 123456, plot = \"png\")");
		//REXP rexp2 = engine.eval("NMFAnalysis(expFile,geneFile,directory,clusterNum = 2,method = \"brunet\",nrun = 10,seed = 123456)");				
		//System.out.println("ConsensusClusterPlusAnalysis="+rexp);
		//System.out.println("NMFAnalysis="+rexp2);
		engine.end();
	}
	/**
	 * 工作目录必需先创建好，系统不会自动创建目录
	 * 目前没有办法捕捉R的错误信息，如果函数ConsensusClusterPlusAnalysis，NMFAnalysis返回了null,基本上就是发生错误了
	 * 建议先写R脚本调试，没问题后再用JRI写一遍
	 * 工作目录中要创建sum data目录，否则输出的文件会很少，因为Themis_custom_method等三个方法没有执行
	 * path的路径必需在工作目录里面
	 */
	public static void themis(String expFile,String geneFile,String directory,String path){
		Rengine engine = new Rengine(null, false, null);
		if (!engine.waitForR()) {
            System.out.println("Cannot load R");
            return;
        }
		engine.eval("library(Themis2)");
		engine.eval("library(ConsensusClusterPlus)");
		engine.eval("library(NMF)");
		engine.assign("expFile", expFile);
		engine.assign("geneFile", geneFile);
		engine.assign("directory", directory);
		engine.assign("path", path);		
		engine.eval("exp <- expFile");
		engine.eval("gene <- geneFile");
		engine.eval("dir <- directory");
		engine.eval("p <- path");
		engine.eval("setwd(dir)");
		REXP dir = engine.eval("print(dir)");
		REXP gene = engine.eval("print(gene)");
		REXP exp = engine.eval("print(exp)");
		REXP p = engine.eval("print(p)");
		System.out.println("eval :: print param dir="+dir);	
		System.out.println("eval :: print param gene="+gene);
		System.out.println("eval :: print param exp="+exp);
		System.out.println("eval :: print param p="+p);
		System.out.println("call function ConsensusClusterPlusAnalysis,start.....");
		REXP cr = engine.eval("ConsensusClusterPlusAnalysis(exp, gene, dir,maxK = 9, reps = 1000, pItem = 0.8,clusterNum = 2,clusterAlg = \"km\", distance = \"pearson\", pFeature = 1,seed = 123456, plot = \"png\")");
		System.out.println("call function ConsensusClusterPlusAnalysis,end.....");
		System.out.println("call function ConsensusClusterPlusAnalysis,return value = "+cr);
		System.out.println("call function NMFAnalysis,start.....");
		REXP nr = engine.eval("NMFAnalysis(exp,gene,dir,clusterNum = 2,method = \"brunet\",nrun = 10,seed = 123456)");
		System.out.println("call function NMFAnalysis,end.....");
		System.out.println("call function NMFAnalysis,return value = "+nr);
		engine.eval("Themis_custom_method(dir, p)");
		engine.eval("Themis_process(dir)");
		engine.eval("Themis_output(dir)");
		System.out.println("R program end...");
		engine.end();
	}

}
